package cn.eu.common.query.model;

import cn.eu.common.query.annotation.Query;
import cn.hutool.core.util.StrUtil;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Getter
@Setter
public class QueryConditionModel {

    private String fieldName;

    private List<ConditionModel> conditions = new ArrayList<>();

    public QueryConditionModel(Map<String, TableModel> tableModelMap,
                               String mainTableAlias,
                               String fieldName,
                               Query query) {
        this.fieldName = fieldName;
        Query.Type type = query.type();
        String[] fields = query.field();
        if(fields.length == 0) {
            conditions.add(new ConditionModel(tableModelMap, type, mainTableAlias, fieldName));
        } else {
            for (String field : fields) {
                List<String> fieldInfo = StrUtil.split(field, ".");
                if(fieldInfo.size() == 1) {
                    conditions.add(new ConditionModel(tableModelMap, type, mainTableAlias, fieldInfo.get(0)));
                } else if(fieldInfo.size() == 2) {
                    String alias = StrUtil.blankToDefault(fieldInfo.get(0), mainTableAlias);
                    conditions.add(new ConditionModel(tableModelMap, type, alias, fieldInfo.get(1)));
                }
            }
        }
    }

}
